package 构建模式;

import testJ.test1;

/**
 * 定义了算法族，分别封装起来，让它们之间可以互相替换，此模式的变化独立于算法的使用者
 * 当多个不同类有相同的方法时，可以用策略模式
 */
public class 策略模式 {
    public static void main(test1[] args) {
        //创建一个普通僵尸，在创建的时候指定策略。   这样就能让多个类，共用一类似的方法
        NormalZomie normalZomie = new NormalZomie(new stepByStepMove(),new BiteAttack());
        normalZomie.display();
        normalZomie.move();
        normalZomie.attack();
    }
}
interface Moveable{
    void move();
}
interface Attackable{
    void attack();
}
class stepByStepMove implements Moveable{
    @Override
    public void move() {
        System.out.println("一步一步移动。");
    }
}
class BiteAttack implements Attackable{
    @Override
    public void attack() {
        System.out.println("咬.");
    }
}
abstract class Zombie {
    abstract public void display();
    Moveable moveable;
    Attackable attackable;
    abstract void move();
    abstract void attack();


    public Zombie(Moveable moveable, Attackable attackable) {
        this.moveable = moveable;
        this.attackable = attackable;
    }

    public Moveable getMoveable() {
        return moveable;
    }

    public void setMoveable(Moveable moveable) {
        this.moveable = moveable;
    }

    public Attackable getAttackable() {
        return attackable;
    }

    public void setAttackable(Attackable attackable) {
        this.attackable = attackable;
    }
}
class NormalZomie extends Zombie{
    public NormalZomie(Moveable moveable, Attackable attackable) {
        super(moveable, attackable);
    }

    @Override
    public void display() {
        System.out.println("我是一个普通僵尸");
    }

    @Override
    void move() {
        moveable.move();

    }

    @Override
    void attack() {
        attackable.attack();
    }
}